package 所有的题类.F.树;

import com.sun.org.apache.regexp.internal.RE;
import 所有的题类.ZA.链表.TreeNode;

import java.nio.channels.Pipe;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 二叉树的最大深度 {

    public static void main(String[] args) {
        int[]array={1,2,3,4,5,6,7,8,9,10,11,12,13};
        TreeNode treeNode = arrayToTreeNode(array, 0, array.length - 1);
        cengXuBianLi(treeNode);
    }

    /**
     * 层序遍历二叉树
     * @param root
     * @return
     */
    public static void cengXuBianLi(TreeNode root){
        Queue<TreeNode> queue = new LinkedList();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode poll = queue.poll();
            System.out.print( poll.val+"->");
            if(poll.right!=null){
                queue.add(poll.right);
            }
            if (poll.left != null) {
                queue.add(poll.left);
            }
        }
    }

    public static TreeNode arrayToTreeNode(int[]array,int left,int right){
        TreeNode root=null;
        if(right>=left){
            int mid=(left+right+1)/2;
            root = new TreeNode(array[mid]);
            TreeNode leftNode = arrayToTreeNode(array, left, mid-1);
            TreeNode rightNode = arrayToTreeNode(array, mid + 1, right);
            root.left=leftNode;
            root.right=rightNode;
        }else{
            root=null;
        }
        return root;
    }

    public static int getDetp(TreeNode root){
        if (root == null) {
            return 0;
        }
        int left=getDetp(root.left);
        int right=getDetp(root.right);
        return Integer.max(left,right)+1;
    }


}
